package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.entity.Followers;
import com.example.pojo.entity.Following;
import com.example.pojo.entity.UserProfile;
import com.example.pojo.vo.UserVo;
import com.example.result.Result;
import com.example.service.IFollowersService;
import com.example.service.IUserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjw
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/followers")
public class FollowersController {
    @Autowired
    private IFollowersService followersService;
    @Autowired
    private IUserProfileService userProfileService;
    //   获取粉丝列表,传用户id过来
    @GetMapping("/id")
  public Result<List<UserVo>> handleGetFollowers(@RequestParam("id") Integer id){
        try{
            //根据用户查询粉丝数量
            LambdaQueryWrapper<Followers> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Followers::getUId, id);
            List<Followers> list = followersService.list(lambdaQueryWrapper);
            List<Integer> ids = new ArrayList<>();
            for (Followers followers1 : list) {
                ids.add(followers1.getFriendId());
            }
            List<UserProfile> userProfiles = userProfileService.listByIds(ids);
            System.out.println(Arrays.toString(userProfiles.toArray()));
            List<UserVo> userVos = userProfiles.stream()
                    .map(userProfile -> new UserVo(
                            userProfile.getName(),
                            userProfile.getDescription(),
                            userProfile.getIndexBg(),
                            userProfile.getAvatar()
                            // 继续添加其他需要的属性赋值
                    ))
                    .collect(Collectors.toList());
            System.out.println(userVos);

            return Result.success(userVos);

        } catch(Exception e){
            return   Result.error();

        }

    }

}
